package croleta.core.file;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class File {
	private ServletContext servletContext;
	private String path;
	
	/**
	 * <b>File:</b><br>
	 * The Class initialization method.<br>
	 * It saves the given path for future use.
	 * @param path the file's path.
	 */
	public File(String path,ServletContext servletContext) {
		this.path = path;
		setServletContext(servletContext);
	}
	
	/**
	 * <b>setServletContext:</b><br>
	 * Sets a {@link ServletContext} Object as a static object for this class.
	 * @param servletContext the {@link ServletContext} to be set for this Class.
	 */
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	/**
	 * <b>fileToString:</b><br>
	 * Scans the file using the {@link ServletContext}'s {@link InputStream}.
	 * @return a <code>String</code> representation of the file.
	 * @throws ServletException if the file is a directory or if there is an IO issue.
	 * @throws IOException
	 */
	public String fileToString() throws ServletException {
		String s = "";
		if (servletContext == null)
			throw new ServletException("no ServletContext has been set.");
		if (isDir())
			throw new ServletException("the file is a directory and can't be read.");
		try {
			InputStream in = servletContext.getResourceAsStream(path);
			while (in.available()>0) {
				s += (char) in.read();
			}
		} catch (IOException e) { throw new ServletException("IO Exception while reading a file", e.getCause());
		}
		return s;
	}

	/**
	 * <b>isDir:</b><br>
	 * It checks if the URI ends with "/".
	 * @return 
	 * 	<b><code>true</code></b> - if the file is a directory.<br>
	 *	<b><code>false</code></b> - if the file is a file.
	 * @throws ServletException if no {@link ServletContext} has been set.
	 */
	public boolean isDir() throws ServletException {
		if (servletContext == null)
			throw new ServletException("no ServletContext has been set.");
		return servletContext.getRealPath(path).endsWith("/");
	}
	
	/**
	 * <b>isFile:</b><br>
	 * returns the opposite of isDir.
	 * @return 
	 * 	<b><code>true</code></b> - if the file is a file.<br>
	 *	<b><code>false</code></b> - if the file is a directory.
	 * @throws ServletException if no {@link ServletContext} has been set.
	 */
	public boolean isFile() throws ServletException {
		return !isDir();
	}
	
	/**
	 * <b>exists:</b><br>
	 * uses the {@link ServletContext} to check if this file exists.
	 * @return 
	 * 	<b><code>true</code></b> - if the file exists.<br>
	 *	<b><code>false</code></b> - if the file doesn't exists or is malformed.
	 * @throws ServletException if no {@link ServletContext} has been set.
	 */
	public boolean exists() throws ServletException {
		if (servletContext == null)
			throw new ServletException("no ServletContext has been set.");
		try {
			return servletContext.getResource(path)!=null;
		} catch (MalformedURLException e) {
			return false;
		}
	}
}
